<template>
  <div class="app-container">
    <el-card>
      <el-row>
        <el-form ref="queryForm" inline size="small" :model="queryForm">
          <el-form-item label="所属院系" prop="deptId">
            <el-select v-model="queryForm.deptId" placeholder="请选择所属院系" filterable>
              <el-option
                v-for="item in deptList"
                :key="item.id"
                :label="item.label"
                :value="item.id">
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="教师姓名" prop="name">
            <el-input placeholder="请输入教师姓名" v-model="queryForm.name"></el-input>
          </el-form-item>
          <el-form-item label="身份证号" prop="idcard">
            <el-input placeholder="请输入身份证号" v-model="queryForm.idcard"></el-input>
          </el-form-item>
          <el-form-item label="手机号" prop="mobile">
            <el-input placeholder="请输入手机号" v-model="queryForm.mobile"></el-input>
          </el-form-item>
          <el-form-item label="工号" prop="jobNumber">
            <el-input placeholder="请输入工号" v-model="queryForm.jobNumber"></el-input>
          </el-form-item>
          <el-button icon="el-icon-search" size="small" type="primary" @click="onClickSearch">搜索</el-button>
          <el-button icon="el-icon-refresh" size="small" @click="resetQueryForm">重置</el-button>
        </el-form>
      </el-row>
      <el-row>
        <el-button icon="el-icon-plus" size="small" type="primary" plain @click="onClickAdd">新增</el-button>
        <el-button icon="el-icon-edit" size="small" type="success" plain @click="onClickEdit()" :disabled="editDisabled">修改</el-button>
        <el-button icon="el-icon-edit" size="small" type="danger" plain>删除</el-button>
      </el-row>
    </el-card>
    <el-table
      v-loading="loading"
      :data="list"
      element-loading-text="加载中"
      border
      fit
      highlight-current-row
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55"></el-table-column>
      <el-table-column align="center" label="ID" width="95" prop="id"></el-table-column>
      <el-table-column label="所属院系" prop="dept.name" width="200"></el-table-column>
      <el-table-column label="教师姓名" prop="name" width="100"></el-table-column>
      <el-table-column label="身份证号" prop="idcard" width="180"></el-table-column>
      <el-table-column label="手机号" prop="mobile" width="120"></el-table-column>
      <el-table-column label="工号" prop="jobNumber" width="120"></el-table-column>
      <el-table-column fixed="right" label="操作" width="200">
        <template slot-scope="scope">
          <el-button type="text" size="small" @click="handleEdit(scope.row.id)">编辑</el-button>
          <el-button type="text" size="small" @click="resetPassword(scope.row.id)">重置密码</el-button>
          <el-button type="text" size="small" @click="handleDelete(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="page"
      :page-sizes="pageSizes"
      :page-size="pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total">
    </el-pagination>
    <el-drawer
      :title="drawer.title"
      :visible.sync="drawer.show"
      :before-close="onDrawerClose"
      size="50%">
      <el-form :rules="formRules" ref="editForm" label-width="auto" :model="form" size="medium">
        <el-form-item label="所属院系" prop="deptId">
          <el-select v-model="form.deptId" placeholder="请选择所属院系" filterable>
            <el-option
              v-for="item in deptList"
              :key="item.id"
              :label="item.label"
              :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="姓名" prop="name">
          <el-input v-model="form.name" placeholder="请输入教师姓名"></el-input>
        </el-form-item>
        <el-form-item label="入职日期" prop="entryDate">
          <el-date-picker format="yyyy-MM-dd" value-format="yyyy-MM-dd" placeholder="请选择教师入职日期" v-model="form.entryDate"></el-date-picker>
        </el-form-item>
        <el-form-item label="身份证号" prop="idcard">
          <el-input v-model="form.idcard" placeholder="请输入教师身份证号"></el-input>
        </el-form-item>
        <el-form-item label="手机号" prop="mobile">
          <el-input v-model="form.mobile" placeholder="请输入教师手机号"></el-input>
        </el-form-item>
<!--        <el-form-item label="工号" prop="jobNumber">
          <el-input v-model="form.jobNumber" placeholder="请输入教师工号"></el-input>
        </el-form-item>-->
<!--        <el-form-item label="简介" prop="descr">
          <el-input v-model="form.descr" placeholder="请输入教师简介"></el-input>
        </el-form-item>-->
      </el-form>
      <div class="form-drawer-footer">
        <el-button @click="onDrawerClose">取 消</el-button>
        <el-button type="primary" @click="onDrawerConfirm" :loading="drawer.confirmLoading">
          {{ drawer.confirmLoading ? '提交中 ...' : '确 定' }}
        </el-button>
      </div>
    </el-drawer>
  </div>
</template>

<script>
import {addTeacher, deleteOneTeacher, getTeacherList, getOneTeacher, updateTeacher, resetTeacherPassword} from "@/api/teacher";
import List from "@/mixins/list";
import {getDeptSimpleList} from "@/api/dept";
import teacherValidator from "@/validator/TeacherValidator";

export default {
  data() {
    return {
      queryListFun: getTeacherList,
      addFun: addTeacher,
      updateFun: updateTeacher,
      queryOneFun: getOneTeacher,
      deleteOneFun: deleteOneTeacher,
      validator: teacherValidator,
      queryForm: {
        deptId: null,
        name: '',
        idcard: '',
        mobile: '',
        jobNumber: ''
      },
      form: {
        name: '',
        entryDate: null,
        idcard: '',
        mobile: '',
        jobNumber: '',
        deptId: null,
        descr: ''
      },
      name: '教师',
      deptList: []
    }
  },
  mixins: [
    List
  ],
  mounted() {
    getDeptSimpleList({}).then(res => {
      const data = []
      for (let i of res.data) {
        data.push({
          id: i.id,
          label: i.name
        })
      }
      this.deptList = data
    })
  },
  methods: {
    resetPassword(id) {
      const loading = this.$loading({
        lock: true,
        text: 'Loading',
        spinner: 'el-icon-loading',
        background: 'rgba(0, 0, 0, 0.7)'
      });
      resetTeacherPassword(id).then(res => {
        this.$message({
          type: 'success',
          message: res.msg
        })
      }).finally(() => {
        loading.close()
      })
    }
  }
}
</script>

<style scoped>

</style>
